import { ref } from 'vue'
import { getSparePartApplyDetailNoLogin, getSparePartApplyDetailLogin } from '@/api/store'

export default function () {
  const getSparePartApplyDetailLoading = ref(false)
  const sparePartApplyDetail = ref<WarehouseAPIResponse.SparePartApplyDetail>({
    id: '',
    code: '',
    saleNo: null,
    networkId: '',
    networkName: '',
    factory: null,
    status: 1,
    type: 1,
    receivingStoreId: null,
    receivingStoreName: '',
    shippingStoreId: null,
    shippingStoreName: '',
    contactName: '',
    contactPhone: '',
    orderRemark: '',
    province: null,
    city: null,
    county: null,
    town: null,
    detailAddress: '',
    createType: 1,
    creator: null,
    createTime: '',
    updateTime: '',
    freezeAmount: null,
    isPosting: 0,
    materialApplyVoList: [],
    area: null,
    spareApplyOrderLogList: [],
    attachmentList: [],
    allWarehouseList: [],
    networkCode: '',
  })

  const getSparePartApplyDetail = async (id: string, isLogin = true) => {
    try {
      getSparePartApplyDetailLoading.value = true
      const fn = isLogin ? getSparePartApplyDetailLogin : getSparePartApplyDetailNoLogin
      const res = await fn(id)
      getSparePartApplyDetailLoading.value = false
      if (res.data.code === 20000) {
        sparePartApplyDetail.value = res.data.data.row
        return ''
      }

      return res.data.message
    } catch (e) {
      getSparePartApplyDetailLoading.value = false
      return '获取配件申请单详情失败，请稍后重试'
    }
  }

  return {
    sparePartApplyDetail,
    getSparePartApplyDetailLoading,
    getSparePartApplyDetail,
  }
}
